#include "Udpserver.hpp"
#include "Route.hpp"
#include <memory>
#include <string>
#include "InetAddr.hpp"
#include "ThreadPool.hpp"

using namespace wxd;

using task_t = std::function<void()>;
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "usage: " << argv[0] << " port" << std::endl;
        exit(1);
    }

    // std::string ip = argv[1];
    u_int16_t port = std::stoi(argv[1]);

    // 创建Route对象
    std::shared_ptr<Route> ptr_route = std::make_shared<Route>();
    // Route route;

    // // 将Route的方法传给server
    // std::unique_ptr<UdpServer> ptr_server = std::make_unique<UdpServer>(port,[&ptr_route](int sockfd,InetAddrTrans& user,const std::string& info,const std::string& message){
    //     ptr_route->SendAll(sockfd,user,info,message);
    // });

    // 引入线程池，服务器收到任务后交由线程池处理
    ThreadPool<task_t> *tp = ThreadPool<task_t>::GetInstance();
    std::unique_ptr<UdpServer> ptr_server = std::make_unique<UdpServer>(port, [&tp, &ptr_route](int sockfd, InetAddrTrans &user, const std::string &info, const std::string &message)
                                                                        {
                                                                            task_t task = std::bind(&Route::SendAll, ptr_route, sockfd, user, info, message);
                                                                            //注意如果使用bind想传递智能指针，不要传unique_ptr
                                                                            tp->Enqueue(task); });

    ptr_server->Init();
    ptr_server->Start();

    return 0;
}
